# -*- coding: utf-8 -*-
#
# @Author: CPS
# @email: 373704015@qq.com
# @Date: 2024-04-22 17:21:32.921467
# @Last Modified by: CPS
# @Last Modified time: 2024-04-22 17:21:32.921467
# @file_path "Z:/work\2024\项目\中大模型数据提取处理"
# @Filename "xlsxFileToMike11.py"
# @Description: 功能描述
#
import os, sys

sys.path.append("..")

import pandas as pd
from pydantic import BaseModel
from Types import TemplateData


TEMPLATE = """
{top_id}
{river_section_id}
                   0
COORDINATES
    0
FLOW DIRECTION
    0      
PROTECT DATA
    0      
DATUM
     0.000
RADIUS TYPE
    0
DIVIDE X-Section
0
SECTION ID
    {section_id}
INTERPOLATED
    0

ANGLE    0.00   0
RESISTANCE NUMBERS
   1  1     {Mann}     {Mann}     {Mann}    {Mann}    {Mann}
PROFILE        {section_xz_len}
{section_xz_data}
LEVEL PARAMS
   0  0    0.000  0    0.000   0
*******************************
"""


XZ_TEMPLATE = "   {X}    {Z}     {Mann}     <#0>     0     0.000     0\n"

COLS_NAME = {
    "河道名称": "A",
    "河段名称": "B",
    "断面号": "C",
    "糙率": "G",
    "X": "H",
    "Z": "I",
    "左间距": "D",
    "中间距": "E",
    "右间距": "F",
}


def test():
    global COLS_NAME
    result = ""
    test_file = r"./参考和相关资料/第二部分解析excel导出成mike11/副本断面数据（错误标星，留空）编辑.xlsx"

    data = pd.read_excel(
        test_file,
        names=COLS_NAME.values(),
        header=0,
    )

    i = 0
    top_id = "HEC_TO_MIKE11"
    pre_river_section_id = None
    section_xz_data = ""
    xz_len = 0
    for index, each_row in data.iterrows():
        print("index: ", index)
        print("each_row: ", each_row)

        xz_len += 1
        section_id = str(each_row[COLS_NAME["断面号"]])
        river_section_id = (
            f'{str(each_row[COLS_NAME["河道名称"]])}_'
            f'{str(each_row[COLS_NAME["河段名称"]])}_'
            f'{str(each_row[COLS_NAME["断面号"]])}'
        )
        Mann = str(each_row[COLS_NAME["糙率"]])
        section_xz_len = str(xz_len)
        section_xz_data += XZ_TEMPLATE.format(
            **{"X": str(each_row[COLS_NAME["X"]]), "Z": str(each_row[COLS_NAME["Z"]]), "Mann": Mann}
        )
        format_data = {
            "top_id": top_id,
            "river_section_id": river_section_id,
            "section_id": section_id,
            "Mann": Mann,
            "section_xz_data": section_xz_data,
            "section_xz_len": section_xz_len,
        }

        if river_section_id != pre_river_section_id:
            # 下一个断面
            pre_river_section_id = river_section_id

            # 保存上一个断面到结果中
            result += TEMPLATE.format(**format_data)

            # 清空数据
            section_xz_data = ""
        break

    print("result: ", result)

    return result

    # for each_row in data:
    #     print("each_row: ", each_row)
    #     # river_section_id = (
    #     #     f'{str(each_row[COLS_NAME["河道名称"]])}_'
    #     #     f'{str(each_row[COLS_NAME["河段名称"]])}_'
    #     #     f'{str(each_row[COLS_NAME["断面号"]])}'
    #     # )

    #     # print("river_section_id: ", river_section_id)

    #     i += 1
    #     if i == 3:
    #         break


if __name__ == "__main__":
    test()
